package com.hillky.emos.wx.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hillky.emos.wx.domain.TbMeeting;
import com.hillky.emos.wx.exception.EmosException;
import com.hillky.emos.wx.service.TbMeetingService;
import com.hillky.emos.wx.mapper.TbMeetingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* @author Hillky
* @description 针对表【tb_meeting(会议表)】的数据库操作Service实现
* @createDate 2023-01-29 10:56:24
*/
@Service
public class TbMeetingServiceImpl extends ServiceImpl<TbMeetingMapper, TbMeeting>
    implements TbMeetingService{

    @Autowired
    private TbMeetingMapper meetingMapper;

    @Override
    public ArrayList<HashMap> searchMyMeetingListByPage(HashMap param) {
        List<HashMap> list = meetingMapper.searchMyMeetingListByPage(param);
        String date=null;
        ArrayList resultList = new ArrayList();
        HashMap resultMap=null;
        JSONArray array=null;
        //按日期进行分类
        for (HashMap map : list) {
            String temp = map.get("date").toString();
            if(!temp.equals(date)){
                date=temp;
                resultMap=new HashMap();
                resultMap.put("date",date);
                array=new JSONArray();
                resultMap.put("list",array);
                resultList.add(resultMap);
            }
            array.put(map);
        }
        return resultList;
    }

    @Override
    public HashMap searchMeetingById(Integer id) {
        //查询会议详情
        HashMap map = meetingMapper.searchMeetingById(id);
        //查询参与成员
        ArrayList<HashMap> list = meetingMapper.searchMeetingMembers(id);
        map.put("members", list);
        return map;
    }

    @Override
    public void insertMeeting(TbMeeting entity) {
        boolean res = this.save(entity);
        if(!res){
            throw new EmosException("会议添加失败");
        }
    }

    @Override
    public void deleteMeetingById(Integer id) {
        TbMeeting oldMeeting = this.getById(id);
        DateTime startTime = DateUtil.parse(oldMeeting.getDate() + " " + oldMeeting.getStart());
        DateTime now = DateUtil.date();
        if (now.isAfterOrEquals(startTime.offset(DateField.MINUTE, -20))&&now.isBeforeOrEquals(startTime.offset(DateField.MINUTE, 20))) {
            throw new EmosException("距离会议开始不足20分钟，不能删除会议");
        }
        boolean res = this.removeById(id);
        if(!res){
            throw new EmosException("删除会议失败");
        }
    }

    @Override
    public ArrayList<HashMap> searchApprovalMeetingByPage(HashMap param) {
        return (ArrayList<HashMap>) meetingMapper.searchApprovalMeetingByPage(param);
    }

    @Override
    public List<String> searchUserMeetingInMonth(HashMap param) {
        List<String> list=meetingMapper.searchUserMeetingInMonth(param);
        return list;
    }


}




